package com.prothink.controller;


import com.prothink.entity.JsProduct;
import com.prothink.entity.RequestData;
import com.prothink.service.JsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @ClassName : JsProductController  //类名
 * @Description : 商品mvc  //描述
 * @Author : Zhang //作者
 * @Date: 11/21  14:22
 */
@CrossOrigin
@RestController
@RequestMapping("/product")
public class JsProductController {

    @Autowired
    private JsProductService jsProductService;

    /**
     * 获取商品数据
     */
    @RequestMapping("/getProduct")
    public RequestData getCourse() {
        List<JsProduct> courseList = jsProductService.getProduct();
        if(courseList != null){
            return RequestData.buildSuccess(courseList);
        }else {
            return null;
        }
    }
    /**
     * 获取商品数据
     */
    @RequestMapping("/getProductJS")
    public RequestData getProductJS(@RequestBody JsProduct jsProduct) {
        System.out.println(jsProduct);
        List<JsProduct> productList = jsProductService.getProductJS(jsProduct);
        if(productList != null){
            return RequestData.buildSuccess(productList);
        }else {
            return null;
        }
    }
    /**
     * 根据订单编号获取商品数据
     */
    @RequestMapping("/getProductByshopId")
    public RequestData getProductByshopId(Integer shopId) {
        System.out.println(shopId);
        JsProduct product = jsProductService.getProductByshopId(shopId);
        if(product != null){
            return RequestData.buildSuccess(product);
        }else {
            return null;
        }
    }

    /**
     * 删除商品信息
     */
    @RequestMapping("/deleteProduct")
    public RequestData deleteCourse(Integer productId) {
        Boolean i = jsProductService.deleteProduct(productId);
        if (i) {
            return RequestData.success();
        }else{
            return null;
        }
    }
    /**
     * 修改商品信息
     */
    @RequestMapping("/updateProduct")
    public RequestData updateProduct(@RequestBody JsProduct jsProduct) {
        Boolean b = jsProductService.updateProduct(jsProduct);
        if (b) {
            return RequestData.buildSuccess(b);
        }else{
            return null;
        }
    }
    /**
     * 上、下架修改
     */
    @RequestMapping("/updProductSate")
    public RequestData updProductSate(@RequestBody JsProduct jsProduct) {
        Boolean i = jsProductService.updProductSate(jsProduct);
        if (i) {
            return RequestData.buildSuccess(i);
        }else{
            return null;
        }
    }

    /*
     * 添加
     */
    @RequestMapping("/addProduct")
    public RequestData addProduct(@RequestBody JsProduct jsProduct){
        jsProduct.setMainImage("images/"+jsProduct.getMainImage());
        jsProduct.setCreateDate(new Date());
        System.out.println(jsProduct);
        Boolean flag = jsProductService.addProduct(jsProduct);
        if (flag){
            return RequestData.buildSuccess(jsProduct);
        }
        return null;
    }
}
